package com.yy.tankgame;
/**
 * 
 *使用配置文件properties
 *@date 2014-9-8
 *@version 2.9
 */

import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class TankClient extends Frame {

	Tank myTank = new Tank(50, 50, true, Direction.STOP, this);
//	Tank enemyTank = new Tank(100, 100, false, Tank.Direction.STOP, this);
	// Missile m = null; //改用list
	List<Missile> missiles = new ArrayList<Missile>();
	List<Explode> explode = new ArrayList<Explode>();
	List<Tank> eTanks = new ArrayList<Tank>();
	
	Blood b = new Blood();
	Wall w1 = new Wall(100, 200, 20, 150, this), w2 = new Wall(300, 100, 300, 20, this);

	Explode e = new Explode(200, 200, this);
	public static final int GAME_WIDTH = 800, GAME_HEIGHT = 600;
	Image offScreenImage = null;


	public void update(Graphics g) {

		if (offScreenImage == null) {
			offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
		}
		Graphics gOffScreen = offScreenImage.getGraphics();
		Color c = gOffScreen.getColor();
		gOffScreen.setColor(Color.black);
		gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
		gOffScreen.setColor(c);
		paint(gOffScreen);
		g.drawImage(offScreenImage, 0, 0, null);

	}
	
	public void paint(Graphics g){
		
		//test statement
		g.drawString("missiles	number " + missiles.size(), 10, 50);
		g.drawString("explodes	number " + explode.size(), 10, 70);
		g.drawString("tanks 	number " + eTanks.size(), 10, 90);
		g.drawString("tanks		life" + myTank.getLife(), 10, 110);

		myTank.draw(g);
		myTank.eat(b);
		
		w1.draw(g);
		w2.draw(g);
		b.draw(g);
//		enemyTank.draw(g);
		for(int i = 0; i < missiles.size(); i++){
			Missile m = missiles.get(i);
//			m.hitTank(enemyTank);
			m.hitTanks(eTanks);
			m.hitTank(myTank);
			m.draw(g);
			
			m.hitWall(w1);
			m.hitWall(w2);
		}		
		for(int i = 0; i < explode.size(); i++){
			Explode e = explode.get(i);
			e.draw(g);
		}
//		敌人死光,触发重载敌人坦克
		if(eTanks.size()== 0){
			for(int i = 0; i < Integer.parseInt(PropertyMgr.getProperty("reProduceTankcount")); i++){
				Tank t = new Tank(50 + 40*(i+1), 50, false, Direction.D, this);
				this.eTanks.add(t);
			}
		}
		for(int i = 0; i < eTanks.size(); i++){
			Tank t = eTanks.get(i);
			
			t.collideWithWall(w1);
			t.collideWithWall(w2);
			t.collideWithTanks(eTanks);
			t.draw(g);
		}
		
	}



	public void launchFrame() {
		int initTankCount = Integer.parseInt(PropertyMgr.getProperty("initTankCount"));
		
		this.setLocation(400, 300);
		this.setSize(GAME_WIDTH, GAME_HEIGHT);
		this.setTitle("TankWar");
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				super.windowClosing(e);
				System.exit(0);
			}
		});
		this.setResizable(false);
		this.setBackground(Color.black);
		
		
		
		for(int i = 0; i < initTankCount; i++){
			Tank t = new Tank(50 + 40*(i+1), 50, false, Direction.D, this);
			this.eTanks.add(t);
		}

		this.addKeyListener(new KeyMoniter());
		setVisible(true);

		new Thread(new PaintThread()).start();

	}

	public static void main(String[] args) {
		TankClient tc = new TankClient();
		tc.launchFrame();
		
	}

	private class PaintThread implements Runnable {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			while (true) {
				repaint();

				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}

	}

	private class KeyMoniter extends KeyAdapter {

		@Override
		public void keyReleased(KeyEvent e) {
			super.keyReleased(e);
			myTank.keyReleased(e);
		}

		@Override
		public void keyPressed(KeyEvent e) {
			// TODO Auto-generated method stub
			super.keyPressed(e);
			myTank.keyPressed(e);

		}
	}

}
